package com.sf.tools.generate;
import java.io.File;

import com.sf.tools.common.Common;





public class BuildDownloadAction {

	private static StringBuffer buff = new StringBuffer();
	
	public static void createDownloadAction(){
		buff = new StringBuffer();
		buff.append("package com.sf.module."+Common.MODULE_NAME+".action;").append("\n")
		.append("").append("\n")
		.append("import java.io.ByteArrayInputStream;").append("\n")
		.append("import java.io.File;").append("\n")
		.append("import java.io.FileInputStream;").append("\n")
		.append("import java.io.InputStream;").append("\n")
		.append("import java.io.UnsupportedEncodingException;").append("\n")
		.append("import java.util.Date;").append("\n")
		.append("").append("\n")
		.append("import org.slf4j.Logger;").append("\n")
		.append("import org.slf4j.LoggerFactory;").append("\n")
		.append("").append("\n")
		.append("import com.sf.framework.server.base.action.BaseAction;").append("\n")
		.append("import com.sf.module.cdpmgmt.util.DateUtils;").append("\n")
		.append("import com.sf.module.gswmgmt.excel.ExcelNameConfig;").append("\n")
		.append("import com.sf.module.gswmgmt.excel.ExcelNameConfig.LabelType;").append("\n")
		.append("import com.sf.module.gswmgmt.util.CommonUtil;").append("\n")
		.append("").append("\n")
		.append("/**").append("\n")
		.append("* <pre>").append("\n")
		.append("* *********************************************").append("\n")
		.append("* Copyright sf-express.").append("\n")
		.append("* All rights reserved.").append("\n")
		.append("* Description: 报表文件公共下载 Action处理类").append("\n")
		.append("* HISTORY").append("\n")
		.append("* *********************************************").append("\n")
		.append("*  ID     DATE          PERSON          REASON").append("\n")
		.append("*  1      2013-04-24    sfhq217         创建").append("\n")
		.append("* *********************************************").append("\n")
		.append("* </pre>").append("\n")
		.append("*/").append("\n")
		.append("public class DownloadAction extends BaseAction {").append("\n")
		.append("").append("\n")
		.append("    private static final long serialVersionUID = 1L;").append("\n")
		.append("").append("\n")
		.append("    private final Logger log = LoggerFactory.getLogger(DownloadAction.class);").append("\n")
		.append("    ").append("\n")
		.append("\t").append("/**").append("\n")
		.append("\t").append(" * Ajax请求成功的响应标志").append("\n")
		.append("\t").append(" */").append("\n")
		.append("\t").append("private Boolean success=true;").append("\n")
		.append("\t").append("").append("\n")
		.append("\t").append("private InputStream inputStream;").append("\n")
		.append("\t").append("").append("\n")
		.append("\t").append("private String fileName;").append("\n")
		.append("\t").append("").append("\n")
		.append("\t").append("private String msg;").append("\n")
		.append("\t").append("").append("\n")
		.append("\t").append("private String tplType;").append("\n")
		.append("\t").append("").append("\n")
		.append("\t\t").append("private boolean isAddTime;//是否添加时间").append("\n")
		.append("\t").append("").append("\n")
		.append("\t").append("/**").append("\n")
		.append("\t").append(" * 下载模板文件").append("\n")
		.append("\t").append(" * @return").append("\n")
		.append("\t").append(" */").append("\n")
		.append("\t").append("public String downloadTpl(){").append("\n")
		.append("\t\t").append("try {").append("\n")
		.append("\t\t\t").append("fileName = ExcelNameConfig.getExcelName(tplType, LabelType.Name)+\".xls\";").append("\n")
		.append("\t\t\t").append("inputStream = CommonUtil.getDownloadStream(fileName);").append("\n")
		.append("            return SUCCESS;").append("\n")
		.append("        }catch (Exception e) {").append("\n")
		.append("            success = false;").append("\n")
		.append("            msg = e.getMessage();").append("\n")
		.append("            if (\"\".equals(msg) || null == msg) {").append("\n")
		.append("                msg = e.toString();").append("\n")
		.append("                log.info(\"exception\", e);").append("\n")
		.append("            } else {").append("\n")
		.append("                log.debug(\"exception\", e);").append("\n")
		.append("            }").append("\n")
		.append("            return ERROR;").append("\n")
		.append("        }").append("\n")
		.append("\t").append("}").append("\n")
		.append("\t").append("").append("\n")
		.append("\t").append("/**").append("\n")
		.append("\t").append(" * 下载导出的文件").append("\n")
		.append("\t").append(" * @return").append("\n")
		.append("\t").append(" */").append("\n")
		.append("\t").append("public String downloadFile(){").append("\n")
		.append("\t\t").append("if (fileName == null || \"\".equals(fileName.trim())) {").append("\n")
		.append("            return ERROR;").append("\n")
		.append("        }").append("\n")
		.append("        try {").append("\n")
		.append("\t").append("        fileName = java.net.URLDecoder.decode(fileName, \"utf-8\");").append("\n")
		.append("\t").append("        File file = new File(CommonUtil.getTempdir() + File.separator + fileName);").append("\n")
		.append("            FileInputStream fis = new FileInputStream(file);").append("\n")
		.append("            byte[] data = new byte[fis.available()];").append("\n")
		.append("            fis.read(data);").append("\n")
		.append("            inputStream = new ByteArrayInputStream(data);").append("\n")
		.append("            fis.close();").append("\n")
		.append("            fileName = fileName.substring(fileName.lastIndexOf(\"~\") + 1); ").append("\n")
		.append("            if(isAddTime){").append("\n")
		.append("\t").append("            fileName = fileName.split(\".\")[0] + \"_\"").append("\n")
		.append("\t\t\t\t").append("+ DateUtils.getDateStr(\"yyyyMMdd\", new Date())").append("\n")
		.append("\t\t\t\t").append("+ \".xls\";").append("\n")
		.append("            }").append("\n")
		.append("            if (!file.delete()) {").append("\n")
		.append("\t\t\t\t").append("log.error(\"delete report file failure, file Path:\" + file.getPath());").append("\n")
		.append("\t\t\t").append("}").append("\n")
		.append("            return SUCCESS;").append("\n")
		.append("        } catch (Exception e) {").append("\n")
		.append("            log.error(\"downloadReport failure\", e);").append("\n")
		.append("            return ERROR;").append("\n")
		.append("        }").append("\n")
		.append("\t").append("}").append("\n")
		.append("\t").append("").append("\n")
		.append("\t").append("/**").append("\n")
		.append("\t").append(" * 获取Ajax请求成功的响应标志").append("\n")
		.append("\t").append(" */").append("\n")
		.append("\t").append("public Boolean getSuccess() {").append("\n")
		.append("\t\t").append("return success;").append("\n")
		.append("\t").append("}").append("\n")
		.append("\t").append("").append("\n")
		.append("\t").append("/**").append("\n")
		.append("\t").append(" * 设置inputStream").append("\n")
		.append("\t").append(" */").append("\n")
		.append("\t").append("public InputStream getInputStream() {").append("\n")
		.append("\t\t").append("return inputStream;").append("\n")
		.append("\t").append("}").append("\n")
		.append("\t").append("/**").append("\n")
		.append("\t").append(" * 获取fileName").append("\n")
		.append("\t").append(" */").append("\n")
		.append("\t").append("public void setFileName(String fileName) {").append("\n")
		.append("\t\t").append("this.fileName = fileName;").append("\n")
		.append("\t").append("}").append("\n")
		.append("\t").append("/**").append("\n")
		.append("\t").append(" * 设置fileName").append("\n")
		.append("\t").append(" */").append("\n")
		.append("\t").append("public String getFileName() {").append("\n")
		.append("\t\t").append("try {").append("\n")
		.append("\t\t\t").append("fileName = new String(fileName.getBytes(\"GBK\"), \"ISO8859-1\");").append("\n")
		.append("\t\t").append("} catch (UnsupportedEncodingException e) {").append("\n")
		.append("\t\t\t").append(" log.error(\"set fileName to GBK Encoding failure\", e);").append("\n")
		.append("\t\t").append("}").append("\n")
		.append("\t\t").append("return fileName;").append("\n")
		.append("\t").append("}").append("\n")
		.append("").append("\n")
		.append("\t").append("public String getMsg() {").append("\n")
		.append("\t\t").append("return msg;").append("\n")
		.append("\t").append("}").append("\n")
		.append("").append("\n")
		.append("\t").append("public void setMsg(String msg) {").append("\n")
		.append("\t\t").append("this.msg = msg;").append("\n")
		.append("\t").append("}").append("\n")
		.append("").append("\n")
		.append("\t").append("public void setTplType(String tplType) {").append("\n")
		.append("\t\t").append("this.tplType = tplType;").append("\n")
		.append("\t").append("}").append("\n")
		.append("").append("\n")
		.append("\t").append("public void setAddTime(boolean isAddTime) {").append("\n")
		.append("\t\t").append("this.isAddTime = isAddTime;").append("\n")
		.append("\t").append("}").append("\n")
		.append("\t").append("").append("\n")
		.append("}").append("\n");

		String content = buff.toString();
		
		File file = new File(Common.SAVE_PATH+File.separator+"action");
		if(!file.exists()){
			file.mkdirs();
		}
		Common.write(new File(file,"DownloadAction.java"), content);
		
		System.out.println("DownloadAction.java 已生成......");
		
	}
	
}
